package com.sk.restree.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.sk.restree.model.dto.StoreDTO;
import com.sk.restree.model.ibatis.SqlMapFactory;

public class StoreDAO {
	private static StoreDAO instance;
	private List<StoreDTO> stores;
	static {
		instance = new StoreDAO();
	}

	private StoreDAO() {
		stores = new ArrayList<StoreDTO>();
		init();
	}

	private void init() {
		SqlMapClient sqlMap = SqlMapFactory.getSqlMapInstance();
		setStoreList(sqlMap);
	}

	public static StoreDAO getIntance() {
		return instance;
	}

	@SuppressWarnings("unchecked")
	public List<StoreDTO> setStoreList(SqlMapClient sqlMap) {
		try {
			stores = sqlMap.queryForList("selectStoreList", null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stores;
	}

	public List<StoreDTO> getStoreList() {
		if(stores.size()==0)
			init();
		return stores;
	}

	public StoreDTO getStore(String storeName) {
		if(stores.size()==0)
			init();
		for (StoreDTO store : stores) {
			if (store.getStore_name().equals(storeName.trim()))
				return store;
		}
		return null;
	}

}
